package com.cg.asm.client;

import com.alibaba.fastjson.JSON;
import com.fasterxml.uuid.Generators;
import com.cg.asm.client.domain.http.HttpClientResult;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class AsmUUID {

    /**
     * 生成唯一编码
     *
     * @return
     */
    public static String generate() {
        return Generators.randomBasedGenerator().generate().toString();
    }

    /**
     * 密码加密
     *
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    public static String pwdEnCoder(HttpServletRequest httpServletRequest, String password) {
        String returnValue = "";
        String hostUrl = httpServletRequest.getHeader("Host-Url");
        if (StringUtils.isNotEmpty(hostUrl)) {
            Map<String, String> param = new HashMap<>();
            param.put("requestUrl", httpServletRequest.getRequestURI());
            param.put("password", password);
            try {
                String getUrl = hostUrl + "/auth/session/pwdEnCoder";
                HttpClientResult httpClientResult = HttpClientUtils.doGet(getUrl, param);
                Map<String, Object> data = JSON.parseObject(httpClientResult.getContent());
                if (httpClientResult.getCode() == 200) {
                    if ("success".equals(data.get("returnType"))) {
                        returnValue = (String) data.get("returnData");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnValue;
    }
}
